/**
 * Copyright (c) 2013 Mark Tielemans
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * DISCLAIMER
 * This software is provided "as is" and any expressed or implied warranties, including, but not limited to, the implied warranties of 
 * merchantability and fitness for a particular purpose are disclaimed. In no event shall the regents or contributors be liable for any 
 * direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute 
 * goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in 
 * contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if 
 * advised of the possibility of such damage.
 */
package nl.marktielemans.easydb;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nl.marktielemans.easydb.generator.MySQLQueryGenerator;
import nl.marktielemans.easydb.model.DatabaseTable;
import nl.marktielemans.easydb.model.IndexType;
import nl.marktielemans.easydb.model.TableColumn;

/**
 * Utilities used by EasyDB.
 * 
 * @author Mark Tielemans
 * @version 1.0
 * @since 1.0
 */
public class EasyDBUtils {

	/**
	 * Retrieve a map of columns from the given {@link DatabaseTable} that have an index, in which the 
	 * keys are the {@link IndexType}.
	 * @param table
	 * @return columns by index
	 */
	public static Map<String, List<TableColumn<?>>> getTableColumnsByIndex(DatabaseTable table) {
		Map<String, List<TableColumn<?>>> colsByIndex = new HashMap<String, List<TableColumn<?>>>();
		for(TableColumn<?> col : table.getColumns()) {
			if(col.getIndexType() != null) {
				// Add column to list for indexType, or create new list for indexType
				List<TableColumn<?>> indexCols = colsByIndex.get(col.getIndexType());
				if(indexCols == null) {
					indexCols = new ArrayList<TableColumn<?>>();
				}
				indexCols.add(col);
				colsByIndex.put(col.getIndexType().getSqlText(), indexCols);
			}
		}
		return colsByIndex;
	}

	/**
	 * Retrieves a String representation of a {@link TableColumn}'s value property, 
	 * or <code>null</code>. If the value is a {@link Date}, the default formatting for the default 
	 * locale will be used, unless a dateformat was defined by 
	 * {@link EasyDBConfig#setDbDateFormat(String)}.
	 * @param columnValue
	 * @return value as String
	 */
	public static String columnValueAsString(Object columnValue) {
		Object value = columnValue;
		String converted = null;
		
		// Special checks
		if(value instanceof Date) {
			DateFormat df=DateFormat.getDateInstance();
			if(EasyDBConfig.get().getDbDateFormat()!=null){
				df = new SimpleDateFormat(EasyDBConfig.get().getDbDateFormat());
			}
			converted = df.format(value);
		}
		
		if(converted == null) converted = value.toString();
		MySQLQueryGenerator.logger.debug("Retrieved value '"+converted+"' from '"+columnValue+"'");
		return converted;
	}
	
	/**
	 * Find a {@link DatabaseVendor} with the given vendorId if it is available.
	 * @param vendorId
	 * @return the {@link DatabaseVendor} with the given id, or <code>null</code> if it's unknown
	 */
	public static DatabaseVendor getVendor(String vendorId) {
		for(DatabaseVendor vendor : DatabaseVendor.getVendors()) {
			if(vendor.getVendorId().equalsIgnoreCase(vendorId)) {
				return vendor;
			}
		}
		return null;
	}
}
